﻿using DevelopTool.Common;
using DevelopTool.Model.ChatModels;
using Sunny.UI;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace DevelopTool.Forms.ChatForms
{
    /// <summary>
    /// 用户列表项控件
    /// </summary>
    public partial class ChatUserItemControl : UIUserControl
    {
        private UIMenuStyle uIMenuStyle = Setting.Theme.ControlThemColor;

        private ChatUser chatUser;

        /// <summary>
        /// 设置或获取用户信息实体
        /// </summary>
        public ChatUser ChatUser
        {
            get
            {
                return this.chatUser;
            }
            set
            {
                this.chatUser = value;
                this.SetUserInfo(value);
            }
        }

        private DateTime? time = DateTime.Now;

        #region 事件

        //单击事件参数
        public delegate void ClickHandler(object sender,MouseButtons button,ChatUser user);
        /// <summary>
        /// 单击事件
        /// </summary>
        [Description("单击事件"), Category("DevelopTool")]
        public event ClickHandler ItemClick;


        //双击事件参数
        public delegate void DoubleClickHandler(object sender,ChatUser user);
        /// <summary>
        /// 双击事件
        /// </summary>
        [Description("双击事件"), Category("DevelopTool")]
        public event DoubleClickHandler ItemDoubleClick;


        #endregion 事件

        public ChatUserItemControl(ChatUser chatUser,DateTime? time)
        {
            InitializeComponent();
            this.chatUser = chatUser;
            this.time = time;
        }

        private void ChatUserItemControl_Load(object sender,EventArgs e)
        {
            ThemHelper.SetSystemThem(this);

            this.SetUserInfo(this.chatUser);
        }


        private void SetUserInfo(ChatUser chatUser)
        {
            this.Tag = chatUser;
            this.TagString = chatUser.UserId;

            #region 日期

            this.lblTime.ForeColor = Color.Silver;

            if(this.time == null)
            {
                this.lblTime.Visible = false;
            }
            else
            {
                string timeStr = "";

                DateTime t = (DateTime)this.time;
                DateTime n = DateTime.Now;

                if(t.Date == n.Date)
                {
                    timeStr = t.ToString("HH:mm");
                }
                else
                {
                    if(t.Year != n.Year)
                    {
                        timeStr += t.ToString("yyyy") + "-";
                    }
                    else
                    {
                        timeStr += t.ToString("MM-dd HH:mm");
                    }
                }

                this.lblTime.Text = timeStr;
            }
            #endregion 日期


            Color lineColor = Color.Green;

            if(chatUser.IsLine)
            {
                lineColor = Color.Green;
            }
            else
            {
                lineColor = Color.Gray;
            }
            this.uiAvatar.ForeColor = lineColor;

            //有备注时显示备注，没有备注则显示昵称
            this.lblNikeName.Text = string.IsNullOrEmpty(chatUser.RemarkName) ? chatUser.NikeName : chatUser.RemarkName;

            //如果昵称和备注都没有，那么就只显示ip，隐藏备注lable
            if(string.IsNullOrEmpty(this.lblNikeName.Text))
            {
                this.lblNikeName.Visible = false;
                this.uiPanel1.Dock = DockStyle.Fill;
            }
            else
            {
                this.lblNikeName.Visible = true;
                this.uiPanel1.Dock = DockStyle.Bottom;
            }

            this.lblIP.Text = chatUser.IP;

            string r = Environment.NewLine;

            //用户信息
            string userInfo = $"{r}状态：{(chatUser.IsLine ? "在线" : "离线")}{r}{r}昵称：{chatUser.NikeName}{r}{r}端口号：{chatUser.Port}{r}{r}计算机名称：{chatUser.ComputerName}{r}{r}系统用户名称：{chatUser.SystemUserName}{r}{r}系统版本：{chatUser.SystemOsName}{r}{r}应用名称：{chatUser.AppName}";
            this.uiToolTip.SetToolTip(this.uiAvatar,userInfo,chatUser.IP,559933,32,lineColor);
        }

        private void lblNikeName_MouseClick(object sender,MouseEventArgs e)
        {
            ItemClick?.Invoke(this,e.Button,this.chatUser);
        }

        private void ChatUserItemControl_DoubleClick(object sender,EventArgs e)
        {
            ItemDoubleClick?.Invoke(this,this.chatUser);
        }


        private void ChatUserItemControl_MouseHover(object sender,EventArgs e)
        {
            this.BackColor = Color.Gray;
            var controls = this.GetAllControls();
            foreach(Control control in controls)
            {
                control.BackColor = Color.Gray;

                if(control is UIPanel panel)
                {
                    panel.FillColor = Color.Gray;
                }
            }
        }

        private void ChatUserItemControl_MouseLeave(object sender,EventArgs e)
        {
            Color backColor = Color.White;

            switch(uIMenuStyle)
            {
                case UIMenuStyle.Black:
                    backColor = ThemHelper.CustomBackColor_Black;
                    break;
                case UIMenuStyle.White:
                    backColor = ThemHelper.CustomBackColor_White;
                    break;
            }

            this.BackColor = backColor;
            var controls = this.GetAllControls();
            foreach(Control control in controls)
            {
                control.BackColor = backColor;

                if(control is UIPanel panel)
                {
                    panel.FillColor = Color.Gray;
                }
            }
        }


        private void ChatUserItemControl_MouseMove(object sender,MouseEventArgs e)
        {
            ChatUserItemControl_MouseHover(sender,e);
        }


    }
}
